using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using System.Linq;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint.WebPartPages;
using System.Collections.Generic;

namespace SlamCms.SharePoint.Features.PublishingHomeSites
{
	[Guid("405c0113-dc42-40a8-82ee-71f2d7508f74")]
	public class PublishingHomeSitesEventReceiver : SPFeatureReceiver
	{
		public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
		{
			base.FeatureDeactivating(properties);

			SPWeb web = (SPWeb)properties.Feature.Parent;

			using (SPLimitedWebPartManager webPartManager = web.GetLimitedWebPartManager(web.RootFolder.ServerRelativeUrl.TrimEnd('/') + "/default.aspx", PersonalizationScope.Shared))
			{
				List<System.Web.UI.WebControls.WebParts.WebPart> webPartsToDelete = new List<System.Web.UI.WebControls.WebParts.WebPart>();
				foreach (System.Web.UI.WebControls.WebParts.WebPart webPart in webPartManager.WebParts)
				{
					webPartsToDelete.Add(webPart);
				}

				foreach (System.Web.UI.WebControls.WebParts.WebPart webPart in webPartsToDelete)
				{
					webPartManager.DeleteWebPart(webPart);
				}
			}
		}
	}
}
